package com.ilink.ossservice.controller;


import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.log.method.MethodLog;
import com.ilink.ilinkcommon.service.oss.MinioService;
import com.ilink.ilinkcommon.service.oss.OssService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <h3>new-ilink-java-backend</h3>
 *
 * @Description <p>OssController</p>
 * @Author Orleans
 * @Date 2024-09-27 14:26
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/oss")
public class OssController {
    @Autowired
    private final OssService  ossService;

    private final MinioService minioService;

    /**
     * @Author YangZhongjun
     * @Description 查找前端静态图片
     * @Date 15:23 2024/9/27
     * @Param [bucketName指定桶名, filename文件名称]
     * @return String
     **/
    @MethodLog("查找前端静态图片")
    @GetMapping("/get_fIcon")
    public R getFIcon(@RequestParam List<String> filename) {
        try {
            Map<String, String> urlMap = new HashMap<>();
            for (String name : filename) {
                urlMap.put(name, ossService.getFrontendIconUrl("ilink-frontend-resource-migrate", "assets/" + name));
            }
            return R.ok(urlMap);
        } catch (Exception e) {
            return R.error("获取资源图片URL时发生错误：" + e.getMessage());
        }
    }
    @MethodLog("前端根据路径获取对应图片")
    @GetMapping("/get_fIcon_by_path")
    public R getFIconByPath(@RequestParam String path) {
        try {
            return R.ok(ossService.getFileUrl(path));
        } catch (Exception e) {
            return R.error("获取资源图片URL时发生错误：" + e.getMessage());
        }
    }

    @GetMapping("/minio")
    public R minioTest(@RequestParam String objectName, @RequestParam String bucketName) {
        try {
            return R.ok(minioService.generatePresignedUrl(objectName, bucketName));
        } catch (Exception e) {
            return R.error("获取资源图片URL时发生错误：" + e.getMessage());
        }
    }


}
